package test.test_7_23;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class Demo06 {
    /*
    2, 如果List中存储了多个User对象, 去除重复元素(多个重复元素仅保留一个)
    (假设使用Collection和List的api)
     */
    public static void main(String[] args) {
        List<User1> list = new ArrayList<>();
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("wzx",25));
        list.add(new User1("ooo",25));
        Iterator<User1> it = list.iterator();

        List<User1> list1 = new ArrayList<>();
        while(it.hasNext()) {
            User1 user1 = it.next();
            if(!list1.contains(user1)) {
                list1.add(user1);
            }
        }
        System.out.println(list1);

    }
}
class User1 {
    String name;
    int age;

    public User1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User1 user1 = (User1) o;
        return age == user1.age && Objects.equals(name, user1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "User1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
